#include "tcp_server.h"
#include <stdlib.h>

TcpServer::TcpServer(QObject *parent,int port)
        : QTcpServer(parent)
{
//    fortunes << tr("Searching for people...")
//             << tr("You've find a people. Try say hello!")
//             << tr("You've disconnected.");

    QString ip_addr;
    QList<QHostAddress> ip_addr_list = QNetworkInterface::allAddresses();

    for (int i = 0; i < ip_addr_list.size(); ++i)
    {
        if(ip_addr_list.at(i) != QHostAddress::LocalHost &&  ip_addr_list.at(i).toIPv4Address())
        {
            ip_addr = ip_addr_list.at(i).toString();
            break;
        }
    }
    if (ip_addr.isEmpty())ip_addr = QHostAddress(QHostAddress::LocalHost).toString();

    qDebug() << "local ip = " << ip_addr;

    this->listen(QHostAddress(ip_addr),port);
}

void TcpServer::incomingConnection(int socket_descriptor)
{
    TcpSocket *tcp_client_socket = new TcpSocket(this);

    connect(tcp_client_socket,SIGNAL(signal_receive_data(QString,int,int)),this,SLOT(slot_receive_data(QString,int,int)));
    connect(tcp_client_socket,SIGNAL(signal_receive_data(QByteArray,int)),this,SLOT(slot_receive_data(QByteArray,int)));
    connect(tcp_client_socket,SIGNAL(signal_disconnected(int)),this,SLOT(slot_disconnected(int)));

    tcp_client_socket->setSocketDescriptor(socket_descriptor);
    tcp_socket_list.append(tcp_client_socket);
/*
    onlineDescriptor=socketDescriptor;
    QString fortune = fortunes.at(qrand() % fortunes.size());
//    QString fortune = fortunes.at(0);
    ChatThread *thread = new ChatThread(socketDescriptor, fortune, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
*/
}


void TcpServer::slot_receive_data(QString msg, int length,int socket_desc)
{
//    for(int i = 0;i < tcp_socket_list.count();i++)
//    {
//        QTcpSocket *item = tcp_socket_list.at(i);

//        if(item->write(msg.toLatin1(),length) != length)
//        {
//            continue;
//        }
//    }

    emit signal_receive_data(msg,length,socket_desc);
}

void TcpServer::slot_receive_data(QByteArray arr,int socket_desc)
{
    emit signal_receive_data(arr,socket_desc);
}



void TcpServer::slot_disconnected(int descriptor)
{
    for (int i=0;i < tcp_socket_list.count();i++)
    {
        QTcpSocket *item = tcp_socket_list.at(i);

        if(item->socketDescriptor() == descriptor)
        {
            tcp_socket_list.removeAt(i);
            return;
        }
    }
    return;
}

void TcpServer::tcp_server_write_data(QByteArray arr,int socket_desc)
{
    for (int i=0;i < tcp_socket_list.count();i++)
    {
        QTcpSocket *item = tcp_socket_list.at(i);

        if(item->socketDescriptor() == socket_desc)
        {
            item->write(arr);
        }
    }
}

void TcpServer::push_message()
{
//    QString testString;
//    testString="fuck u";
//    slot_receive_data(testString,testString.length());
 /*
   ChatThread *thread = new ChatThread(onlineDescriptor, testString, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
*/
}





